package ch.fhnw.ams.sudoku.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import ch.fhnw.ams.sudoku.SudokuGame.SudokuState;

/**
 * A panel for the play, solve and check buttons. The visibility of the buttons
 * depends on {@link SudokuState}. This panel contains also the status bar.
 * 
 * @author amsler
 * 
 */
public class ControlPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private static final Color LABEL_COLOR = Color.BLUE;
	
	private final JLabel infoLabel;
	private final JButton playButton;
	private final JButton solveButton;
	private final JButton checkButton;
	private final JPanel cards;
	
	
	/**
	 * Creates a new {@link ControlPanel}
	 * 
	 * @param playAction
	 *            the Action used to specify a button
	 * @param solveAction
	 *            the Action used to specify a button
	 * @param checkAction
	 *            the Action used to specify a button
	 */
	public ControlPanel(Action playAction, Action solveAction, Action checkAction) {
		setFocusable(false);
		infoLabel = new JLabel();
		infoLabel.setForeground(LABEL_COLOR);
		Font font = infoLabel.getFont().deriveFont(10.0f);
		infoLabel.setFont(font);
		
		//init buttons
		playButton = new JButton(playAction);
		playButton.setFocusable(false);
		solveButton = new JButton(solveAction);
		solveButton.setFocusable(false);
		checkButton = new JButton(checkAction);
		checkButton.setFocusable(false);
		
		//init cards
		JPanel createCard = new JPanel();
		createCard.add(playButton);
		
		JPanel playCard = new JPanel();
		playCard.add(solveButton);
		playCard.add(checkButton);
		
		CardLayout cardLayout = new CardLayout();
		cardLayout.setHgap(2);
		cards = new JPanel(cardLayout);
		cards.add(playCard, SudokuState.PLAYING.name());
		cards.add(createCard, SudokuState.CREATING.name());
		
		layoutComponents();
	} 
	
	private void layoutComponents() {
		GridBagLayout gbLayout = new GridBagLayout();
		gbLayout.columnWeights = new double[]{1.0, 0.0};
		setLayout(gbLayout);
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.LINE_START;
		Insets insets = gbc.insets;
		insets.left = 9;
		add(infoLabel, gbc);
		
		gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.LINE_END;
		add(cards, gbc);
	}
	
	/**
	 * {@link SudokuState#CREATING}: play button is visible
	 * {@link SudokuState#SOLVING}: check and solve button is visible
	 * 
	 * @param sudokuState
	 *            the current state of the game
	 */
	public void showCard(final SudokuState sudokuState) {
		if (sudokuState == SudokuState.CREATING
				|| sudokuState == SudokuState.PLAYING) {
			
			CardLayout cardLayout = (CardLayout)cards.getLayout();
			cardLayout.show(cards, sudokuState.name());
		}
	}
	
	/**
	 * Sets the message for the status bar
	 * 
	 * @param msg
	 *            the message visible for the user
	 */
	public void setInfoMessage(final String msg) {
		if (SwingUtilities.isEventDispatchThread()) {
			infoLabel.setText(msg);
		} else {
			SwingUtilities.invokeLater(new Runnable() {
				
				@Override
				public void run() {
					infoLabel.setText(msg);
				}
			});
		}
	}

}
